package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.AuditDto;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.enums.common.AuditEnum;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.enums.content.CourseAuditEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.service.AuditService;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class AuditServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements AuditService {

    @Autowired
    private CourseBaseService courseBaseService;
//todo:之后可以改一下放在课程基础信息的审核枚举类中
    private static final Long companyId = AuditEnum.COMPANY_ID;

    @Override
    public PageVO<CourseBaseDTO> queryAuditList(PageRequestParams params, QueryCourseModel model) {
        PageVO<CourseBaseDTO> pageVO = courseBaseService.queryCourseList(params, model, companyId);
        return pageVO;
    }

    @Override
    @Transactional
    public void modifyAuditStatus(AuditDto auditDto) {
        //关键数据
        if (ObjectUtils.isEmpty(auditDto.getCourseBaseId())) {
            ExceptionCast.cast(ContentErrorCode.E_120009);
        }

        if (ObjectUtils.isEmpty(auditDto.getAuditMind())) {
            ExceptionCast.cast(ContentErrorCode.E_120027);
        }

        if (ObjectUtils.isEmpty(auditDto.getAuditStatus())) {
            ExceptionCast.cast(ContentErrorCode.E_120028);
        }
        //业务数据
        LambdaQueryWrapper<CourseBase> wrapper = Wrappers.lambdaQuery(CourseBase.class);
        wrapper.eq(CourseBase::getId, auditDto.getCourseBaseId());
        CourseBase courseBase = this.getOne(wrapper);
        if (com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isEmpty(courseBase)) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }

        Integer status = courseBase.getStatus();
        if (status.equals(CommonEnum.DELETE_FLAG.getCodeInt())) {
            ExceptionCast.cast(ContentErrorCode.E_120022);
        }

        String auditStatus = courseBase.getAuditStatus();
        if (!(auditStatus.equals(CourseAuditEnum.AUDIT_COMMIT_STATUS.getCode()))) {
            ExceptionCast.cast(ContentErrorCode.E_120029);
        }

        //只能赋值审核状态和审核未通过两种状态
        String auditStatu = auditDto.getAuditStatus();
        if (auditStatu.equals(CourseAuditEnum.AUDIT_PUBLISHED_STATUS.getCode())
        ||auditStatu.equals(CourseAuditEnum.AUDIT_COMMIT_STATUS.getCode())
        ||auditStatu.equals(CourseAuditEnum.AUDIT_UNPAST_STATUS.getCode())) {
            ExceptionCast.cast(ContentErrorCode.E_120016);
        }

        //修改课程
        LambdaUpdateWrapper<CourseBase> updateWrapper = Wrappers.lambdaUpdate(CourseBase.class);
        updateWrapper.set(CourseBase::getAuditDate, LocalDateTime.now())
                .set(CourseBase::getAuditStatus, auditStatu)
                .set(CourseBase::getAuditNums, courseBase.getAuditNums() + 1)
                .set(CourseBase::getAuditMind, auditDto.getAuditMind())
                .eq(CourseBase::getId, auditDto.getCourseBaseId());

        boolean update = courseBaseService.update(updateWrapper);
        if (!update) {
            ExceptionCast.cast(ContentErrorCode.E_120030);
        }
    }
}
